<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace  app\index\controller;
use app\api\controller\EmailSend;
use app\index\model\CardsTran;
use app\index\model\UserCheck;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use app\index\model\User as UserModel;
use app\BaseController;
use app\api\controller\OssUp;
use app\index\model\Cards as cardsModel;

/**
 * 首页管理
 * Class Index
 * @package app\admin\controller
 */
class Index extends  BaseController
{

    public $localhost_ip;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $username = '';
        if (session('?user')) {
            $user_list = session('user');
            $username = $user_list['username'];
        }
        $this->localhost_ip = ip();
        View::assign('username_index', $username);
    }

    /**
     * 首页
     * @author 救火队队长
     */
    public function index()
    {
        if (session('?user')) {
            cookie('redirect_url', request()->url(true), 3600);
            return $this->redirect(url('/index/home/index'));
        }
        $getValue = getSetting('open_close_register');
        View::assign([
            'open_close_reg' => $getValue,
        ]);
        return View::fetch();
    }

    /**
     * 判断是否登陆
     * @author 救火队队长
     */
    public function checkLogin(){
        $userModel = new UserModel();
        $result = $userModel->toLogin(input('param.'));

        if($result['status'] === true){
            $url = url("home/index");
            $datas = [
                'href' =>  $url->build(),
            ];
            return show(0,"",$datas);
        }
        $status = ($result['status'] == '100010') ? '100010':-1;
        return show($status,$result['msg'],[]);
    }

    public function emailSend()
    {
        $username       = input('post.username');
        $send_type      = input('post.send_type');
        $check_email    = check_param($username , 'email');
        if(!$check_email) return json(['code'=>0,'msg'=>"邮箱格式错误!"]);

        $current_user_code_time = \session("user_{$username}_{$send_type}_time");
        $current_time           = getUtcTime();

        if($current_time - $current_user_code_time < 5*60){
            return json(['code'=>0,'msg'=>"验证码有效期为5分钟，请检查您的邮箱."]);
        }

        $user_list      = Db::name('user')->where([
            'username'  =>$username,
            'status'    => 1
        ])->find();
        if(!$user_list) return json(['code'=>0,'msg'=>"此帐户已被禁用或不存在."]);

        //获取随机6位字母
        $round_num     = nonceStr();
        //$send_type = check_login|check_pwd
        $result     = EmailSend::sendEmail($send_type , [
            'email'     => $username ,
            'code'      => $round_num,
            'desc'      => 'user send email'
        ]);
        if($result){
            \session("user_{$username}_{$send_type}", $round_num);
            \session("user_{$username}_{$send_type}_time", $current_time);
            return json(['code'=>1,'msg'=>"已发送邮件验证码."]);
        }else{
            return json(['code'=>0,'msg'=>"验证码发送失败!"]);
        }
    }

    public function signIn()
    {
        $getValue = getSetting('open_close_register');
        if($getValue == 1){
            return $this->redirect(url('/index'));
        }
        return View::fetch();
    }

    public function editPwd()
    {
        return View::fetch();
    }

    public function UpOssLoads()
    {
        $file = request()->file('file');

        $suffix_list = explode("." , $file->getOriginalName());
        $suffix      = $suffix_list[1] ?? 'jpg';
        $result   = OssUp::fileUp([
            'type'      => 'document',
            'tmp_name'  => $file->getRealPath(),
        ],$suffix);
        $blog       = $result['blog'];
        $img_url    = $result['img_url'];
        if($blog){
            $img_name_list = explode('/',$img_url);
            $img_name      = !empty($img_name_list)? end($img_name_list):'';
            return json([
                'code'      => 1,
                'img_url'   => $img_url,
                'img_name'  => $img_name,
                'msg'       => 'upload success',
            ]);
        }else{
            return json([
                'code'      => 0,
                'img_url'   => '',
                'img_name'  => '',
                'msg'       => 'upload error',
            ]);
        }
    }

    public function regNotice()
    {
        return View::fetch();
    }
}